<?php
/**
 * Created by PhpStorm.
 * User: qhzhyt
 * Date: 2018/10/15
 * Time: 15:29
 */

if (!isset($_POST['mapId'])) {
    echo(json_encode(['status' => 0, 'code' => 2]));
    exit(1);
}

session_start();
if (!isset($_SESSION['user'])) {
    echo(json_encode(['status' => 0, 'code' => -1]));
    exit(1);
}
require_once '../conf/conf.php';
$user = $_SESSION['user'];

$mapId = $_POST['mapId'];

if ($db->get('map', '*', ['map_id' => $mapId]) == null) {
    echo(json_encode(['status' => 0, 'code' => 5]));
    exit(1);
}

if ($db->get('collection', '*', ['user_id' => $user['user_id'], 'map_id' => $mapId]) == null) {

    $db->insert('collection', ['user_id' => $user['user_id'], 'map_id' => $mapId]);

    echo(json_encode(['status' => 1, 'code' => 1]));

} else {
    $db->delete('collection', ['user_id' => $user['user_id'], 'map_id' => $mapId]);

    echo(json_encode(['status' => 1, 'code' => 2]));

}